package b2.widget.sendMail
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.ObjWithNum;
	import b2.player.dataModel.Tank;
	import b2.player.event.ObjWithNumEvent;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;


	public class SendGiftDialog extends Sprite
	{
    	//卖出坦克时候起作用
    	private var tankId:int;
    	private var objectIndex:int;
    	private var basicLevel:int;
    	private var strengthenLevel:int;
    	private var numSel:NumberSelecter;
    	private var price:int;
    	public var gift:ObjWithNum;
    	private var buttonOK:BasicButton;

		public function SendGiftDialog(gift:ObjWithNum)
		{
			super();
			this.gift = gift;
			this.tankId = tankId;
			if(gift.obj != null)
			{
				this.objectIndex = gift.obj.objectIndex;
				this.basicLevel = gift.obj.basicLevel;
				this.strengthenLevel = gift.obj.strengthenLevel;
				if (gift.obj is Tank)
					this.tankId = (gift.obj as Tank).tankId;
				else
					this.tankId = 0;
			}
			
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			//透明背景
			var bgr:Sprite = new Sprite();
			var gfx:Graphics = bgr.graphics;
			gfx.beginFill(0x000000, 0.01);
			gfx.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			gfx.endFill();
			addChild(bgr);
			//背景
			var PurchaseDialog:Class = ResourceHelper.getInstance().getClassByName("MessageBox0");
			var dialogSprite:Sprite = new PurchaseDialog();
			dialogSprite.x = (BFont.stageWidth - dialogSprite.width) / 2;
			dialogSprite.y = 140;
			addChild(dialogSprite);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "选择";
			titleText.x = (dialogSprite.width - titleText.width) / 2;
			titleText.y = 15;
			dialogSprite.addChild(titleText);
			//物品图标
			var ClassImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var imageSprite:Sprite = new ClassImage();
			//齐天大圣
			if (objectIndex == 10015)
			{
				imageSprite.width = 192;
				imageSprite.height = 215;
			}
			//死亡骑士
			else if (objectIndex == 10020)
			{
				imageSprite.width = 168;
				imageSprite.height = 168;
			}
			else
			{
				imageSprite.width = 140;
				imageSprite.height = 140;
			}
			imageSprite.x = 40;
			imageSprite.y = 70;
			dialogSprite.addChild(imageSprite);
			
			//加载名字
			var name:TextField = new TextField();
			name.selectable = false;
			name.autoSize = TextFieldAutoSize.CENTER;
			name.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true);
			//装备
			if (objectConfig.type == 1)
			{
				var equipConfig:EquipConfig = objectConfig as EquipConfig;
				name.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的";
			}
			name.appendText(objectConfig.name);
			name.x = 110 - name.width / 2;
			name.y = 215;
			dialogSprite.addChild(name);		
			//加载具体描述
			var description:TextField = new TextField();
			description.multiline = true;
			description.selectable = false;
			description.autoSize = TextFieldAutoSize.LEFT;
			description.width = 200;
			description.wordWrap = true;
			description.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			var descrip:String = objectConfig.description;
			//装备
			if (objectConfig.type == 1)
			{
				descrip = equipConfig.getDescription(0);
			}
			description.htmlText = descrip;
			description.x = 210;
			description.y = 140;
			dialogSprite.addChild(description);
			
			//加载数量选择器
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			
				var me:PlayerInfo = PlayerInfoManager.getInstance().me;
				//坦克
				if (objectConfig.type == 0)
				{
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, 1, 1, 1);
				}
				else
				{
					var objectNum:int = gift.num;
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, objectNum, 1, objectNum);
				}
				numSel.setNumChangeCallback(onNumSelChange);
			dialogSprite.addChild(numSel);
			numSel.x = 280;
			numSel.y = 110;
			
			var gap:int = -60; 
			//确定
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton;
			
			buttonOK = new BasicButton(ButtonBgr1, "选择");
			buttonOK.x = 225;
			buttonOK.y = 230;
			dialogSprite.addChild(buttonOK);
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOk);
			//取消
			var buttonCancel:BasicButton = new BasicButton(ButtonBgr1, "取消");
			buttonCancel.x = 325;
			buttonCancel.y = 230;
			dialogSprite.addChild(buttonCancel);
			buttonCancel.addEventListener(MouseEvent.CLICK, onButtonCancelMouseClick);
			gift.addEventListener(ObjWithNumEvent.UPDATE_NUM, onUpdateGiftNum);
		}
		
		private function onButtonOk(event:Event):void
		{
			var mail:Mail = SendMailTab.getInstance().mail;
			mail.addGift(gift);
			close();
		}
		
		private function onButtonCancelMouseClick(event:MouseEvent):void
        {
        	this.parent.removeChild(this);
        }
        
        private function onUpdateGiftNum(event:ObjWithNumEvent):void
        {
        	numSel.setNumChangeCallback(null);
        	numSel.value = gift.num;
        	numSel.setNumChangeCallback(onNumSelChange);
        }
        
        private function onNumSelChange():void
        {
        	gift.setNum(numSel.value);
        }
        
        public function close():void
        {
        	if (this.parent != null)
        		this.parent.removeChild(this);
        }
	}
}